package com.infitecs.eyas.quality.service.Imp;

import com.infitecs.eyas.boiler.dao.EfficiencyDao;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.combustion.dao.BurnDao;
import com.infitecs.eyas.combustion.dao.ChimneyDao;
import com.infitecs.eyas.combustion.dao.CleanDao;
import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.model.CalculateChimney;
import com.infitecs.eyas.combustion.model.CalculateClean;
import com.infitecs.eyas.combustion.service.BurnService;
import com.infitecs.eyas.combustion.service.ChimneyService;
import com.infitecs.eyas.combustion.service.CleanService;
import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.expression.dao.ExpressionDao;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.mill.dao.MillSelectDao;
import com.infitecs.eyas.mill.dao.PipeCalculateDao;
import com.infitecs.eyas.mill.model.MillSelect;
import com.infitecs.eyas.mill.model.PipeCalculate;
import com.infitecs.eyas.mill.service.MillSelectService;
import com.infitecs.eyas.mill.service.PipeCalculateService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.quality.dao.AshDao;
import com.infitecs.eyas.quality.dao.BurstDao;
import com.infitecs.eyas.quality.dao.ElementDao;
import com.infitecs.eyas.quality.dao.IndustryDao;
import com.infitecs.eyas.quality.model.Ash;
import com.infitecs.eyas.quality.model.Burst;
import com.infitecs.eyas.quality.model.Element;
import com.infitecs.eyas.quality.model.Industry;
import com.infitecs.eyas.quality.service.BurstService;
import com.infitecs.eyas.runcoalbin.dao.CoalBunkerDao;
import com.infitecs.eyas.runcoalbin.model.CoalBunker;
import com.infitecs.eyas.runcoalbin.service.CoalBunkerService;
import com.infitecs.eyas.security.service.UserService;
import com.infitecs.eyas.util.DoubleUtil;
import com.infitecs.eyas.util.ExcelInfo;
import com.infitecs.eyas.util.ExcelReplaceData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xidea.el.Expression;
import org.xidea.el.impl.ExpressionImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: chenkaiyu
 * Date: 15-4-24
 * Time: 下午5:30
 * To change this template use File | Settings | File Templates.
 */
@Service("burstService")
@Transactional
public class BurstServiceImp implements BurstService {
    @Autowired
    private ExpressionDao expressionDao;
    @Autowired
    private BurstDao burstDao;
    @Autowired
    private ElementDao elementDao;
    @Autowired
    private IndustryDao industryDao;
    @Autowired
    private AshDao ashDao;
    @Autowired
    private EfficiencyDao efficiencyDao;
    @Autowired
    private EfficiencyService efficiencyService;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private CoalBunkerDao coalBunkerDao;
    @Autowired
    private CoalBunkerService coalBunkerService;
    @Autowired
    private BurnService burnService;
    @Autowired
    private BurnDao burnDao;
    @Autowired
    private CleanService cleanService;
    @Autowired
    private CleanDao cleanDao;
    @Autowired
    private ChimneyDao chimneyDao;
    @Autowired
    private ChimneyService chimneyService;
    @Autowired
    private MillSelectDao millSelectDao;
    @Autowired
    private MillSelectService millSelectService;
    @Autowired
    private PipeCalculateService pipeCalculateService;
    @Autowired
    private PipeCalculateDao pipeCalculateDao;
    @Autowired
    private PowerDao powerDao;
    @Autowired
    private UserService userService;
    @Override
    public Double calculateTheValueOfKvTi(Burst burst) {
        HashMap<String, Double> map = new HashMap<String, Double>();
        map.put("E21", burst.getHgi());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_5);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        Object E22 = expression.evaluate(map);
        double kvTi = Double.parseDouble(E22.toString());
        burst.setKvti(kvTi);
        burstDao.updateKvTi(burst);
        kvTi = DoubleUtil.getInstance().get4Double(kvTi);
        return kvTi;
    }

    @Override
    public Double calculateTheValueOfKd(Burst burst) { //E80
        Industry industry = industryDao.getIndustryByPowerCaseId(burst.getPowerCaseId());
        Element element = elementDao.getElementByPowerCaseId(burst.getPowerCaseId());
        HashMap<String, Double> map1 = new HashMap<String, Double>();
        map1.put("E6", industry.getAar());
        map1.put("E3", industry.getMar());
        map1.put("E15", element.getQnetAr());
        Formula formula1 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_16);
        Expression expression1 = new ExpressionImpl(formula1.getFormulaDescription());
        Object E75 = expression1.evaluate(map1);
        double num_E75 = Double.parseDouble(E75.toString());

        HashMap<String, Double> map2 = new HashMap<String, Double>();
        map2.put("E9", industry.getVar());
        map2.put("E3", industry.getMar());
        Formula formula2 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_17);
        Expression expression2 = new ExpressionImpl(formula2.getFormulaDescription());
        Object E78 = expression2.evaluate(map2);
        double num_E78 = Double.parseDouble(E78.toString());

        HashMap<String, Double> map3 = new HashMap<String, Double>();
        map3.put("E10", industry.getVdaf());
        map3.put("E75", num_E75);
        map3.put("E78", num_E78);
        Formula formula3 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_18);
        Expression expression3 = new ExpressionImpl(formula3.getFormulaDescription());
        Object E80 = expression3.evaluate(map3);
        double num_E80 = Double.parseDouble(E80.toString());
        burst.setKd(num_E80);
        burstDao.updateKd(burst);
        num_E80 = DoubleUtil.getInstance().get3Double(num_E80);
        return num_E80;
    }

    @Override
    public Double calculateTheValueOfKgc(Burst burst) { //E50
        Industry industry = industryDao.getIndustryByPowerCaseId(burst.getPowerCaseId());
        Element element = elementDao.getElementByPowerCaseId(burst.getPowerCaseId());
        Ash ash = ashDao.getAshByPowerCaseId(burst.getPowerCaseId());
        HashMap<String, Double> map = new HashMap<String, Double>();
        map.put("E30", element.getSar());
        map.put("E6", industry.getAar());
        map.put("E36", ash.getCao());
        map.put("E38", ash.getMgo());
        map.put("E41", ash.getNa2o());
        map.put("E40", ash.getK2o());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_8);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        double kgc = Double.valueOf(expression.evaluate(map).toString());
        burst.setKgc(kgc);
        burstDao.updateKgc(burst);
        kgc = DoubleUtil.getInstance().get3Double(kgc);
        return kgc;
    }

    @Override
    public Double calculateTheValueOfHm(Burst burst) {   //E51
        Industry industry = industryDao.getIndustryByPowerCaseId(burst.getPowerCaseId());
        Ash ash = ashDao.getAshByPowerCaseId(burst.getPowerCaseId());
        HashMap<String, Double> map = new HashMap<String, Double>();
        map.put("E6", industry.getAar());
        map.put("E33", ash.getSio2());
        map.put("E34", ash.getAl2o3());
        map.put("E35", ash.getFe2o3());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_9);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        double hm = Double.parseDouble(expression.evaluate(map).toString());
        burst.setHm(hm);
        burstDao.updateHm(burst);
        hm = DoubleUtil.getInstance().get2Double(hm);
        return hm;
    }

    @Override
    public void updateKe(Burst burst) {
        burstDao.updateKe(burst);
    }

    @Override
    public void updateAi(Burst burst) {
        burstDao.updateAi(burst);
    }

    @Override
    public Burst getBurst(int caseId) {
        return burstDao.getBurstByPowerCaseId(caseId);
    }

    @Override
    public void boomSave(Burst burst) {
        //保存磨损腐蚀爆炸性
        boomSaveData(burst);
        //更新锅炉效率及燃煤量
        Efficiency oldEfficiency = efficiencyDao.getEfficiencyByPowerCaseId(burst.getPowerCaseId());
        OperateField operateField = operateFieldDao.getOperateField(burst.getPowerCaseId());
        if (oldEfficiency != null) {
            oldEfficiency.setE52State(operateField.getE52State());
            oldEfficiency.setE89State(operateField.getE89State());
            oldEfficiency.setE90State(operateField.getE90State());
            oldEfficiency.setE92State(operateField.getE92State());
            oldEfficiency.setE93State(operateField.getE93State());
            oldEfficiency.setE94State(operateField.getE94State());
            oldEfficiency.setE95State(operateField.getE95State());
            efficiencyService.efficiencySaveData(oldEfficiency);
        }
        //更新原煤仓
        CoalBunker oldCoalBunker = coalBunkerDao.getCoalBunker(burst.getPowerCaseId());
        //更新原煤仓信息
        if (oldCoalBunker != null) {
            oldCoalBunker.setE9State(operateField.getE9State());
            coalBunkerService.designSaveData(oldCoalBunker);
        }
        //更新燃烧
        CalculateBurn calculateBurn = burnDao.getCalculateBurnByCaseId(burst.getPowerCaseId());
        if (calculateBurn != null) {
            calculateBurn.setYyglState(operateField.getYyglState());
            calculateBurn.setE54State(operateField.getE54State());
            calculateBurn.setE88State(operateField.getE88State());
            calculateBurn.setE122State(operateField.getE122State());
            calculateBurn.setE155State(operateField.getE155State());
            calculateBurn.setE171State(operateField.getE171State());
            burnService.burnSaveData(calculateBurn);
        }
        //更新烟气净化系统
        CalculateClean calculateClean = cleanDao.getCalculateClean(burst.getPowerCaseId());
        if (calculateClean != null) {
            calculateClean.setE221State(operateField.getE221State());
            calculateClean.setE222State(operateField.getE222State());
            calculateClean.setE231State(operateField.getE231State());
            calculateClean.setE241State(operateField.getE241State());
            calculateClean.setE242State(operateField.getE242State());
            calculateClean.setE248State(operateField.getE248State());
            calculateClean.setE258State(operateField.getE258State());
            cleanService.cleanDataSave(calculateClean);
        }
        //更新烟囱出口直径计算
        CalculateChimney calculateChimney = chimneyDao.getCalculateChimney(burst.getPowerCaseId());
        if (calculateChimney != null) {
            calculateChimney.setE314State(operateField.getE314State());
            calculateChimney.setE315State(operateField.getE315State());
            calculateChimney.setE316State(operateField.getE316State());
            calculateChimney.setCaseId(operateField.getCaseId());
            chimneyService.chimneySaveData(calculateChimney);
        }
        //更新磨煤机选择
        MillSelect millSelect = millSelectDao.getMillSelect(burst.getPowerCaseId());
        if (millSelect != null) {
            millSelect.setMyglState(operateField.getMyglState());
            millSelect.setE12State(operateField.getE12State());
            millSelect.setE74State(operateField.getE74State());
            millSelect.setE75State(operateField.getE75State());
            millSelect.setModelState(operateField.getModelState());
            millSelectService.chooseDataSave(millSelect);
        }
        //更新煤粉管道计算
        PipeCalculate pipeCalculate = pipeCalculateDao.getPipeCalculate(burst.getPowerCaseId());
        if (pipeCalculate != null) {
            pipeCalculateService.pipelineDataSave(pipeCalculate);
        }
    }

    @Override
    public void boomSaveData(Burst burst) {
        //VTI可磨性系数KvtI
        burst.setKvti(calculateKvTi(burst));
        //烟气腐蚀指数Kg.c
        burst.setKgc(calculateKgc(burst));
        //飞灰磨损指数Hm
        burst.setHm(calculateHm(burst));
        //爆炸指数Kd
        burst.setKd(calculateKd(burst));
        //判断新增或者编辑
        Burst oldBurst = burstDao.getBurstByPowerCaseId(burst.getPowerCaseId());
        if (oldBurst == null) {
            burstDao.addBurst(burst);
        } else {
            burstDao.updateBurst(burst);
        }
    }

    @Override
    public void exportMzfx(HttpServletRequest request, HttpServletResponse response, String caseId)throws IOException {
        List<ExcelReplaceData> dataList = new ArrayList<>();
        Power powerTitle = powerDao.getCaseById(caseId);
        String[] userIds=powerTitle.getUserIds().split(",");
        String createId=  userIds[userIds.length-1];
        powerTitle.setCreateId(createId);
        if(userService.getUser(createId)==null){
            powerTitle.setCreateName("");
        }
        powerTitle.setCreateName(userService.getUser(createId).getUsername());
        ExcelInfo.setExcelInfo(1, 4, powerTitle.getPowerName(), dataList);
        ExcelInfo.setExcelInfo(2, 4, powerTitle.getPowerCaseName(), dataList);
        ExcelInfo.setExcelInfo(3, 4, powerTitle.getCreateTime(), dataList);
        ExcelInfo.setExcelInfo(3, 2, powerTitle.getCreateName(), dataList);

        ExcelInfo.setExcelInfo(42, 4, powerTitle.getPowerName(), dataList);
        ExcelInfo.setExcelInfo(43, 4, powerTitle.getPowerCaseName(), dataList);
        ExcelInfo.setExcelInfo(44, 4, powerTitle.getCreateTime(), dataList);
        ExcelInfo.setExcelInfo(44, 2, powerTitle.getCreateName(), dataList);

        ExcelInfo.setExcelInfo(82, 4, powerTitle.getPowerName(), dataList);
        ExcelInfo.setExcelInfo(83, 4, powerTitle.getPowerCaseName(), dataList);
        ExcelInfo.setExcelInfo(84, 4, powerTitle.getCreateTime(), dataList);
        ExcelInfo.setExcelInfo(84, 2, powerTitle.getCreateName(), dataList);

        ExcelInfo.setExcelInfo(122, 4, powerTitle.getPowerName(), dataList);
        ExcelInfo.setExcelInfo(123, 4, powerTitle.getPowerCaseName(), dataList);
        ExcelInfo.setExcelInfo(124, 4, powerTitle.getCreateTime(), dataList);
        ExcelInfo.setExcelInfo(124, 2, powerTitle.getCreateName(), dataList);

        ExcelInfo.setExcelInfo(162, 4, powerTitle.getPowerName(), dataList);
        ExcelInfo.setExcelInfo(163, 4, powerTitle.getPowerCaseName(), dataList);
        ExcelInfo.setExcelInfo(164, 4, powerTitle.getCreateTime(), dataList);
        ExcelInfo.setExcelInfo(164, 2, powerTitle.getCreateName(), dataList);

        ExcelInfo excelInfo =new ExcelInfo();
        excelInfo.setImportName("mzfx.xlsx");
        excelInfo.setOutName("煤质分析报表");
        ExcelReplaceData.replaceModel(dataList, request, response,excelInfo);
    }

    public Double calculateKvTi(Burst burst) {
        HashMap<String, Double> map = new HashMap<String, Double>();
        map.put("E21", burst.getHgi());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_5);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        Object E22 = expression.evaluate(map);
        double kvTi = Double.parseDouble(E22.toString());
        return DoubleUtil.getInstance().get4Double(kvTi);
    }

    public Double calculateKgc(Burst burst) { //E50
        Industry industry = industryDao.getIndustryByPowerCaseId(burst.getPowerCaseId());
        Element element = elementDao.getElementByPowerCaseId(burst.getPowerCaseId());
        Ash ash = ashDao.getAshByPowerCaseId(burst.getPowerCaseId());
        HashMap<String, Double> map = new HashMap<String, Double>();
        map.put("E30", element.getSar());
        map.put("E6", industry.getAar());
        map.put("E36", ash.getCao());
        map.put("E38", ash.getMgo());
        map.put("E41", ash.getNa2o());
        map.put("E40", ash.getK2o());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_8);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        double kgc = Double.valueOf(expression.evaluate(map).toString());
        return DoubleUtil.getInstance().get3Double(kgc);
    }

    public Double calculateHm(Burst burst) {   //E51
        Industry industry = industryDao.getIndustryByPowerCaseId(burst.getPowerCaseId());
        Ash ash = ashDao.getAshByPowerCaseId(burst.getPowerCaseId());
        HashMap<String, Double> map = new HashMap<String, Double>();
        map.put("E6", industry.getAar());
        map.put("E33", ash.getSio2());
        map.put("E34", ash.getAl2o3());
        map.put("E35", ash.getFe2o3());
        Formula formula = expressionDao.getFormulaByName(Constant.FORMULA_NAME_9);
        Expression expression = new ExpressionImpl(formula.getFormulaDescription());
        double hm = Double.parseDouble(expression.evaluate(map).toString());
        return DoubleUtil.getInstance().get2Double(hm);
    }

    public Double calculateKd(Burst burst) { //E80
        Industry industry = industryDao.getIndustryByPowerCaseId(burst.getPowerCaseId());
        Element element = elementDao.getElementByPowerCaseId(burst.getPowerCaseId());
        HashMap<String, Double> map1 = new HashMap<String, Double>();
        map1.put("E6", industry.getAar());
        map1.put("E3", industry.getMar());
        map1.put("E15", element.getQnetAr());
        Formula formula1 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_16);
        Expression expression1 = new ExpressionImpl(formula1.getFormulaDescription());
        Object E75 = expression1.evaluate(map1);
        double num_E75 = Double.parseDouble(E75.toString());

        HashMap<String, Double> map2 = new HashMap<String, Double>();
        map2.put("E9", industry.getVar());
        map2.put("E3", industry.getMar());
        Formula formula2 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_17);
        Expression expression2 = new ExpressionImpl(formula2.getFormulaDescription());
        Object E78 = expression2.evaluate(map2);
        double num_E78 = Double.parseDouble(E78.toString());

        HashMap<String, Double> map3 = new HashMap<String, Double>();
        map3.put("E10", industry.getVdaf());
        map3.put("E75", num_E75);
        map3.put("E78", num_E78);
        Formula formula3 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_18);
        Expression expression3 = new ExpressionImpl(formula3.getFormulaDescription());
        Object E80 = expression3.evaluate(map3);
        double num_E80 = Double.parseDouble(E80.toString());
        return DoubleUtil.getInstance().get3Double(num_E80);
    }

}
